package com.example.PolimiAssistant.common;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.Wsdl2Code.WebServices.PolimiAssistant.PoliUser;
import com.example.PolimiAssistant.R;
import com.example.PolimiAssistant.utils.PreferencesManager;
import com.example.PolimiAssistant.utils.UserDataManager;

/**
 * AsyncUpdateViewTemplate.java
 *
 * This class is the template on how to update the information of the views on an
 * activity, using an asynchronous process.In this case the update is in the
 * onCreate method, but this can be extended to any of the lifecycle methods for
 * an Activity.
 */
public class AsyncUpdateViewTemplate extends Activity {

    /**
     * The tag used for the logs.
     */
    private static final String TAG = AsyncUpdateViewTemplate.class.getSimpleName();

    /**
     * Declare the context of the application
     */
    private final Context context = this;

    public void onCreate(Bundle savedInstanceState) {

        //1. Save the state of the activity
        super.onCreate(savedInstanceState);

        //2. Update the information on the views
        new UpdateData().execute();

        //3. Configure main display options

        //Avoid banding problems with the gradient background
        getWindow().setFormat(PixelFormat.RGBA_8888);

        //Set fixed portrait orientation
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //4. Set the layout used for this activity
        setContentView(R.layout.profile_screen);

        //5. Extra configurations

        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // If your minSdkVersion is 11 or higher, instead use:
        getActionBar().setDisplayHomeAsUpEnabled(true);

        //6. Listeners
    }

    /**
     * This methods is meant to update the UI of the activity after the new
     * data has been retrieved from the updatedata method.
     *
     * @param
     */
    public void updateViews() {

        Log.i(TAG, "Update the information on the views...");

        //Update the corresponding views

    }

    /**
     * This class declares the async process and how it will be executed. The
     * idea of this class is to retrieve data from a place while the app is
     * still running.
     */
    private class UpdateData extends AsyncTask<Void, Void, String>
    {

        //1. Create the loading dialog
        ProgressDialog loadingDialog;
        {
            loadingDialog = new ProgressDialog(AsyncUpdateViewTemplate.this);
        }

        @Override
        protected void onPreExecute() {

            super.onPreExecute();

            //2. Set the message and show the dialog
            loadingDialog.setMessage("Loading...");
            loadingDialog.show();
        }

        @Override
        protected String doInBackground(Void... params) {

            /*
             Here retrieve the information, returning the type of data needed
             to the postExecute process.
             */

            return "return your argument here";

        }

        @Override
        protected void onPostExecute(String arg) {

            super.onPostExecute(arg);

            //Update the corresponding views
            updateViews();

            //Dismiss the loading dialog
            loadingDialog.dismiss();
        }

    }
}
